package part1.q3;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 猫狗队列
 * @author Tommy
 *
 */
public class DogCatQueue {
	private Queue<PetEnterQueue> dogQ;
	private Queue<PetEnterQueue> catQ;
	private long count;   // 用于计数
	public DogCatQueue() {
		this.dogQ = new LinkedList<PetEnterQueue>();
		this.catQ = new LinkedList<PetEnterQueue>();
		this.count = 0;
	}
	// 往猫狗队列中放数据
	public void add(Pet pet) {
		if(pet.getType().equals("dog")) {
			this.dogQ.add(new PetEnterQueue(pet, this.count++));
		} else if(pet.getType().equals("cat")) {
			this.catQ.add(new PetEnterQueue(pet, this.count++));
		} else {
			throw new RuntimeException("<error, not dog or cat>");
		}
	}
	// 从猫狗队列中取数据
	public Pet pollAll() {
		if(!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
			/**
			 * 注意： 想按实际顺序弹出实例，比较两个队列头的时间戳，最更小，代表的更早进入队列的，那么就将哪个弹出
			 */
			if(this.dogQ.peek().getCount() > this.catQ.peek().getCount()) {
				return this.dogQ.poll().getPet();
			} else {
				return this.catQ.poll().getPet();
			}
		} else if(!this.dogQ.isEmpty()) {
			return this.dogQ.poll().getPet();
		} else if(!this.catQ.isEmpty()) {
			return this.catQ.poll().getPet();
		} else {
			throw new RuntimeException("<error, queue is empty.>");
		}
	}
	
	
	public Dog pollDog() {
		if(!this.dogQ.isEmpty()) {
			return (Dog) this.dogQ.poll().getPet();
		} else {
			throw new RuntimeException("<error, Dog queue is empty>");
		}
	}
	
	public Cat pollCat() {
		if(!this.catQ.isEmpty()) {
			return (Cat) this.catQ.poll().getPet();
		} else {
			throw new RuntimeException("<error, Cat queue is empty>");
		}
	}
	
	public boolean isEmpty() {
		return this.dogQ.isEmpty() && this.catQ.isEmpty();
	}
	
	
	public boolean isDogQueueEmpty() {
		return this.dogQ.isEmpty();
	}
	
	public boolean isCatEmpty() {
		return this.catQ.isEmpty();
	}
	
}
